﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace BS_Gimnasio.Seguridad
{
    public abstract class RemoteDownload
    {
        public string UrlString
        {
            get { return UrlString; }
            set { UrlString = value; }
        }

        public string DestDir
        {
            get { return DestDir; }
            set { DestDir = value; }
        }

        public string Usuario
        {
            get { return Usuario; }
            set { Usuario = value; }
        }

        public string Password
        {
            get { return Password; }
            set { Password = value; }
        }

        public RemoteDownload(string urlString, string destDir, string usuario, string password)
        {
            this.UrlString = urlString;
            this.DestDir = destDir;
            this.Usuario = usuario;
            this.Password = password;
        }

        public virtual string DownloadFile()
        {
            return "";
        }
    }

    public class HttpRemoteDownload : RemoteDownload
    {
        public HttpRemoteDownload(string urlString, string descFilePath, string usuario, string password)
            : base(urlString, descFilePath, usuario, password)
        {

        }

        public override string DownloadFile()
        {
            string fileName = System.IO.Path.GetFileName(this.UrlString);
            string descFilePathAndName =
                System.IO.Path.Combine(this.DestDir, fileName);
            try
            {
                WebRequest myre = WebRequest.Create(this.UrlString);
            }

            catch (Exception ex)
            {
                return ex.Message;
            }

            try
            {
                byte[] fileData;
                using (WebClient client = new WebClient())
                {
                    fileData = client.DownloadData(this.UrlString);
                }
                using (FileStream fs =
                      new FileStream(descFilePathAndName, FileMode.OpenOrCreate))
                {
                    fs.Write(fileData, 0, fileData.Length);
                }
                return "Descarga Correcta";
            }

            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }

    public class FtpRemoteDownload : RemoteDownload
    {
        public FtpRemoteDownload(string urlString, string descFilePath, string usuario, string password)
            : base(urlString, descFilePath, usuario, password)
        {

        }

        public override string DownloadFile()
        {
            try
            {
                FtpWebRequest reqFTP;

                string fileName = Path.GetFileName(this.UrlString);
                string descFilePathAndName = Path.Combine(this.DestDir, fileName);
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(this.UrlString);
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.KeepAlive = true;
                reqFTP.Credentials = new NetworkCredential(Usuario, Password);

                using (FileStream outputStream = new FileStream(descFilePathAndName, FileMode.OpenOrCreate))
                using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse())
                using (Stream ftpStream = response.GetResponseStream())
                {
                    int bufferSize = 2048;
                    int readCount;
                    byte[] buffer = new byte[bufferSize];
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                    while (readCount > 0)
                    {
                        outputStream.Write(buffer, 0, readCount);
                        readCount = ftpStream.Read(buffer, 0, bufferSize);
                    }
                }

                return "Descarga Correcta en el directorio";
            }

            catch (Exception)
            {
                return "Error en la Descarga... Verifique tener los permisos correspondientes en el directorio de descarga";
            }
        }
    }
}
